package es.ats.codington.web.servlets;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.Place;
import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.services.EventServiceImpl;
import es.ats.codington.festival.services.PlaceServiceImpl;
import es.ats.codington.festival.services.VisitorServiceImpl;

/**
 * Servlet implementation class CreateEventServlet.
 */
@WebServlet("/CreateEventServlet")
public class CreateEventServlet extends HttpServlet {
	
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;
       
    /**
     * Instantiates a new creates the event servlet.
     *
     * @see HttpServlet#HttpServlet()
     */
    public CreateEventServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * Do get.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws ServletException the servlet exception
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		
	
		HttpSession session = request.getSession(true);
	   	int idV =(Integer) session.getAttribute("VisitorID");

		try{		
			PlaceServiceImpl pla = new PlaceServiceImpl();
			PlaceServiceImpl placeS = new PlaceServiceImpl();		

			ArrayList<Integer> ids = new ArrayList<Integer>();
			HashMap<Integer, String> myPlaces = new HashMap<Integer, String>();

			//obtiene solo ids	
			ids = pla.obtainAllPlaces();
			
			//obtiene ids y names
			myPlaces = placeS.obtainNameAndId();

			session.setAttribute("AllPlaces", ids);
			session.setAttribute("IdsAndNames", myPlaces);
			
			RequestDispatcher rd=request.getRequestDispatcher("registerEvent.jsp");  
	        rd.include(request,response); 
				
		
	    }catch(Exception theException){
	        System.out.println(theException); 
	   }
	}

	/**
	 * Do post.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws ServletException the servlet exception
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession(true);		 
		 EventServiceImpl eve = new EventServiceImpl();
		 int row=0;
		 
         String name = request.getParameter("event");
         String description = request.getParameter("description");
         String end = request.getParameter("horaend");
         String begin = request.getParameter("hora");
         String eventype = request.getParameter("type");
         String idplace = request.getParameter("idplace");
         String capac = request.getParameter("capacity");
         
         String[] idsplaces= idplace.split(" ");
         
         System.out.println(idsplaces[0]);
         
         int placeId = Integer.parseInt(idsplaces[0]);   
         int capacity = Integer.parseInt(capac);

         
      
     //Coge la hora final y la hora incial y saca la duracion del evento
         SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
         
         Date dateEnd = null;
         Date dateBegin = null;
         
         try{
        	 dateEnd = sdf.parse(end);
        	 dateBegin = sdf.parse(begin);
         }
         catch (Exception ex){
             ex.printStackTrace();
         }
         

         long duration = (dateEnd.getTime() - dateBegin.getTime())/60000;
         int dur = (int)duration;
         /*
          *  REGISTER EVENT
          */ 
         try{
         Event event = new Event();
         
         event.setName(name);
         event.setDescription(description);
         event.setDuration(dur);
         event.setTypeEvent(eventype);
         event.setCapacity(capacity);
         event.setIdPlace(placeId);
         
         row= eve.insertEvent(event);
         
         }
         catch (Exception ex){
             ex.printStackTrace();
         }
         
         if(row!=0){
        	 System.out.println("correct");
        //	 response.sendRedirect("listEventAdmin.jsp");
        	 //*RequestDispatcher rd=request.getRequestDispatcher("login.jsp");  
             //*rd.forward(request,response); 
        	 session.setAttribute("Session", 6);
        	 ListAllEventServlet list= new ListAllEventServlet();
        	 list.sendListAllEvent(request, response);

         }
         else{
        	 session.setAttribute("Session", 1);
        	 System.out.println("failed");
        	 response.sendRedirect("registerEvent.jsp");
        	 //*RequestDispatcher rd=request.getRequestDispatcher("home.jsp");  
             //*rd.include(request,response); 
         }
         
	}
	
	

}
